package com.chen.stopwatch.meizu.lap_view;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.chen.stopwatch.meizu.R;

public class ColorSelectorView {
	private Activity mContext = null;

	private Dialog mDlg = null;

	private ListView mListView = null;

	private ColorSelectorAdapter mDataAdapter = null;

	public ColorSelectorView(Activity context) {
		if (null == context)
			return;
		mContext = context;
		mDlg = new Dialog(context);
		mDlg.setContentView(R.layout.color_selector);
		mDlg.setTitle(R.string.please_select_color);
		mListView = (ListView) mDlg.findViewById(R.id.color_selector);
		mDataAdapter = new ColorSelectorAdapter(context);
		mListView.setAdapter(mDataAdapter);
		mListView.setOnItemClickListener(mOnItemClickListener);
	}

	private final OnItemClickListener mOnItemClickListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			if (null != mIColorChangedListener && null != mDataAdapter)
				mIColorChangedListener.notify(mDataAdapter.getColor(arg2));
		}
	};

	private IColorChangedListener mIColorChangedListener = null;

	public void setColorSelectListener(
			IColorChangedListener colorChangedListener) {
		mIColorChangedListener = colorChangedListener;
	}

	public void destroy() {
		mContext = null;
		if (null != mDlg) {
			mDlg.dismiss();
			mDlg = null;
		}

		mListView = null;
		if (null != mDataAdapter) {
			mDataAdapter.destroy();
			mDataAdapter = null;
		}
	}

	public void show() {
		if (null != mDlg)
			mDlg.show();
	}

	public void hide() {
		if (null != mDlg) {
			mDlg.hide();
		}
	}
}
